﻿#include "TableFilterSelectDataBar.h"
#include <QDebug>
#include <QIcon>
#include <QPushButton>
#include "BaseModule/IBaseSignal.h"

namespace pst
{
    TableFilterSelectDataBar::TableFilterSelectDataBar(QWidget* parent)
        : QWidget(parent), ui(new Ui::TableFilterSelectDataBar())
    {
        ui->setupUi(this);
        
    }

    TableFilterSelectDataBar::~TableFilterSelectDataBar()
    {
        delete ui;
    }

    // value在调用前已经去重
    void TableFilterSelectDataBar::addItem(const QString header, const QStringList value, const int index)
    {
        ui->columnName->setText(header.trimmed());
        ui->columnValue->home(false);
        ui->columnValue->setReadOnly(true);
        ui->more->setIcon(QIcon(":/rc/apostrophe.png"));
        int selectedIndex = -1;
        m_result = value;
        for (const QString& str : value)
        {
            m_selectedIndexs.append(++selectedIndex);
            m_valueMap.insert(str.trimmed(), selectedIndex);
        }
        ui->columnValue->setText(value.join(","));
        connect(ui->more, &QPushButton::clicked, this, &TableFilterSelectDataBar::slot_more);
        m_barIndex = index;
    }

    void TableFilterSelectDataBar::pyUpdateItem(const QStringList value)
    {
        ui->columnValue->setText(value.join(","));
        m_selectedIndexs.clear();
        for (QString str : value)
        {
            m_selectedIndexs.append(m_valueMap[str.trimmed()]);
        }
    }

    void TableFilterSelectDataBar::getSelectData(const QModelIndexList& chosenID) 
    {
        QList<int> selectIndexs;
        QStringList contents;
        foreach (const QModelIndex& _index, chosenID)
        {
            selectIndexs.append(_index.row());
            contents.append(_index.data().toString());
        }

        //不在这里判断，应前置判断
        //if (selectIndexs.size() == 0)
        //{
        //    IBaseSignalInstance->signal_sendErrorMessageToUI("变量必须有一个取值，请重新选择数据。");
        //    return;
        //} 
        m_selectedIndexs = selectIndexs;
        ui->columnValue->setText(contents.join(","));
        QHash<int, QStringList>* map = new QHash<int, QStringList>();
        map->insert(m_barIndex, contents);
        emit signal_flushTableBySelectedRow(map);
    }

    void TableFilterSelectDataBar::slot_more()
    {
        m_dataChosenListDialog = new DataChosenListDialog();
        connect(m_dataChosenListDialog, &DataChosenListDialog::signal_sentChosenDataIds, this, &TableFilterSelectDataBar::getSelectData);
        m_dataChosenListDialog->setFixedSize(300, 280);
        m_dataChosenListDialog->setData(m_result);
        m_dataChosenListDialog->updateUI();
        m_dataChosenListDialog->setMultipleSelected(true);
        m_dataChosenListDialog->showWindow();
        m_dataChosenListDialog->setSelctedIndexs(m_selectedIndexs);
    }

    void TableFilterSelectDataBar::updateData(QStringList list)
    {
        m_selectedIndexs.clear();
        for (auto _res : m_result)
        {
            if (list.contains(_res))
            {
                m_selectedIndexs.append(m_result.indexOf(_res));
            }
        }
        list.removeDuplicates();
        ui->columnValue->setText(list.join(","));
    }

} // namespace pst
